﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ECCSystem
{
    public partial class frmSaleCommodity : Form
    {
        public frmSaleCommodity()
        {
            InitializeComponent();
        }
        private BizRule.ClsSaleCommodity objBRSC;
        private void frmSaleCommodity_Load(object sender, EventArgs e)
        {
            try
            {
                this.BindDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void frmSaleCommodity_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frmMain = new frmMain();
            frmMain.Show();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CheckInputItem(txtSaleCommodityName.Text.ToString().Trim()) == true)
                {
                    objBRSC = new ECCSystem.BizRule.ClsSaleCommodity();
                    objBRSC.AddSaleCommodity(txtSaleCommodityName.Text.ToString().Trim());
                    txtSaleCommodityName.Text = "";
                    this.BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                //wether have selected row,if have not selected row, pop-up prompt
                if (dgList.CurrentRow == null)
                {
                    MessageBox.Show("データを選んでください！", "ECC Information");
                    return;
                }
                if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    objBRSC = new ECCSystem.BizRule.ClsSaleCommodity();
                    objBRSC.DeleteSaleCommodity(Convert.ToInt64(dgList.SelectedRows[0].Cells[0].Value));
                    this.BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "ECC Catch Error");
            }

        }

        //bind data
        private void BindDataGrid()
        {
            DataSet objDS = new DataSet();
            objBRSC = new ECCSystem.BizRule.ClsSaleCommodity();
            objDS = (DataSet)objBRSC.GetList();
            //Don't show blank row
            objDS.Tables[0].DefaultView.AllowNew = false;
            dgList.DataSource = objDS.Tables[0];
        }

        //Validate Data 
        private bool CheckInputItem(string strName)
        {
            if (strName == "")
            {
                MessageBox.Show("訴求内容を入力してください！", "ECC Information");
                txtSaleCommodityName.Focus();
                return false;
            }
            return true;
        }
    }
}